package cn.shengchao.event.distribute;

import cn.hutool.core.date.DateUtil;
import cn.shengchao.base.event.PaySuccessMessageEvent;
import cn.shengchao.cap.acl.ports.repositories.IProductThirdCapOrderGradeRepository;
import cn.shengchao.examstar.distribute.ohs.local.appservice.DistributeOrderDomainService;
import cn.shengchao.examstar.pay.acl.adapters.repositories.OrderRepository;
import cn.shengchao.examstar.rpc.distribute.dto.DistributeOrderTaskDto;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeOrderTaskStatusDataStatusEnum;
import cn.shengchao.examstar.rpc.uddsys.bi.PlatformUserArchBI;
import cn.shengchao.examstart.user.acl.adapters.repositories.UserRepository;
import cn.shengchao.ycbdc.acl.ports.repositories.IProductThirdYcbdcUserOrderRepository;
import cn.shengchao.ycbdc.domain.token.YcBdcTokenUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;


@Slf4j
@Component
public class DistributeOrderPaySuccessListener {

    @Autowired
    private PlatformUserArchBI platformUserArchBI;

    @Autowired
    private DistributeOrderDomainService distributeOrderDomainService;
    @Autowired
    private IProductThirdCapOrderGradeRepository orderGradeRepository;
    @Autowired
    private IProductThirdYcbdcUserOrderRepository ycbdcUserOrderRepository;
    @Autowired
    OrderRepository orderRepository;
    @Autowired
    UserRepository userRepository;

    @Autowired
    YcBdcTokenUtil ycBdcTokenUtil;
    @EventListener
    public void handleCustomEvent(PaySuccessMessageEvent event) {
        JSONObject order = JSON.parseObject(JSON.toJSONString(event.getSource()));
        //用户id
        Long userId = order.getLong("userId");
        //产品id
        Long productId = order.getLong("productId");
        //订单号
        String orderId = order.getString("tradeNo");
        //订单号
        String productType = order.getString("productType");
        //价格
        BigDecimal amount = order.getBigDecimal("amount");
        // 订单时间
        Date orderTime = order.getDate("payTime");
        try {
            //目前所有的都要分成，订单产品的购买处理已经转移
            createDistributeOrderTask(userId,productType,amount,orderTime,orderId);
        } catch (Exception e) {
            log.error("订单分销处理失败！orderId：{}", orderId, e);
        }
    }
    /**
     * 创建订单任务
     * @param userId
     * @param productType
     * @param amount
     * @param orderTime
     * @param orderId
     */
    private  void createDistributeOrderTask(Long userId,String productType,BigDecimal amount,Date orderTime, String orderId) {

        //创建订单任务
        DistributeOrderTaskDto distributeOrderTaskDto = new DistributeOrderTaskDto();
        distributeOrderTaskDto.setOrderId(Long.valueOf(orderId));
        distributeOrderTaskDto.setProductType(productType);
        distributeOrderTaskDto.setPayUserId(userId);
        distributeOrderTaskDto.setDistributeOrderAmount(amount);
        distributeOrderTaskDto.setOrderTime(orderTime);
        distributeOrderTaskDto.setDistributeTime(DateUtil.offsetSecond(orderTime, 5));
        distributeOrderTaskDto.setDataStatus(DistributeOrderTaskStatusDataStatusEnum.STATUS_CREATE.getStatus());
        distributeOrderDomainService.createDistributeOrderTask(distributeOrderTaskDto);

    }
}
